<template>
  <el-dialog
    :title="!dataForm.exhibitId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="藏品编号" prop="exhibitNo">
      <el-input v-model="dataForm.exhibitNo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="藏品名称" prop="exhibitName">
      <el-input v-model="dataForm.exhibitName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="照片地址" prop="photo">
      <el-input v-model="dataForm.photo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="藏品分类" prop="exhibitCategories">
      <el-input v-model="dataForm.exhibitCategories" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="出土年份" prop="year">
      <el-input v-model="dataForm.year" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="出土地点" prop="unearthedSite">
      <el-input v-model="dataForm.unearthedSite" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="详细信息" prop="exhibitInfo">
      <el-input v-model="dataForm.exhibitInfo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="点赞数" prop="praises">
      <el-input v-model="dataForm.praises" placeholder=""></el-input>
    </el-form-item>
    <!-- <el-form-item label="" prop="hits">
      <el-input v-model="dataForm.hits" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getExhibitById, saveOrUpdateExhibit } from '@/api/exhibit'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        exhibitId: 0,
        exhibitNo: '',
        exhibitName: '',
        photo: '',
        exhibitCategories: '',
        year: '',
        unearthedSite: '',
        exhibitInfo: '',
        praises: '',
        hits: '',
        createTime: '',
        updateTime: ''
      },
      dataRule: {
        exhibitNo: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        exhibitName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        photo: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        exhibitCategories: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        year: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        unearthedSite: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        exhibitInfo: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        praises: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        hits: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.exhibitId = id || 0
      this.visible = true
      this.$nextTick(async () => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.exhibitId) {
          const { data } = await getExhibitById(this.dataForm.exhibitId)
          if (data && data.code === 0) {
            this.dataForm.exhibitNo = data.exhibits.exhibitNo
            this.dataForm.exhibitName = data.exhibits.exhibitName
            this.dataForm.photo = data.exhibits.photo
            this.dataForm.exhibitCategories = data.exhibits.exhibitCategories
            this.dataForm.year = data.exhibits.year
            this.dataForm.unearthedSite = data.exhibits.unearthedSite
            this.dataForm.exhibitInfo = data.exhibits.exhibitInfo
            this.dataForm.praises = data.exhibits.praises
            this.dataForm.hits = data.exhibits.hits
            this.dataForm.createTime = data.exhibits.createTime
            this.dataForm.updateTime = data.exhibits.updateTime
          }
          // this.$http({
          //   url: `/exhibits/info/${this.dataForm.exhibitId}`,
          //   method: 'get'
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.dataForm.exhibitNo = data.exhibits.exhibitNo
          //     this.dataForm.exhibitName = data.exhibits.exhibitName
          //     this.dataForm.photo = data.exhibits.photo
          //     this.dataForm.exhibitCategories = data.exhibits.exhibitCategories
          //     this.dataForm.year = data.exhibits.year
          //     this.dataForm.unearthedSite = data.exhibits.unearthedSite
          //     this.dataForm.exhibitInfo = data.exhibits.exhibitInfo
          //     this.dataForm.praises = data.exhibits.praises
          //     this.dataForm.hits = data.exhibits.hits
          //     this.dataForm.createTime = data.exhibits.createTime
          //     this.dataForm.updateTime = data.exhibits.updateTime
          //   }
          // })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs.dataForm.validate(async (valid) => {
        if (valid) {
          const exhibit = {
            exhibitId: this.dataForm.exhibitId || undefined,
            exhibitNo: this.dataForm.exhibitNo,
            exhibitName: this.dataForm.exhibitName,
            photo: this.dataForm.photo,
            exhibitCategories: this.dataForm.exhibitCategories,
            year: this.dataForm.year,
            unearthedSite: this.dataForm.unearthedSite,
            exhibitInfo: this.dataForm.exhibitInfo,
            praises: this.dataForm.praises,
            hits: this.dataForm.hits,
            createTime: this.dataForm.createTime,
            updateTime: this.dataForm.updateTime
          }
          const { data } = await saveOrUpdateExhibit(this.dataForm.exhibitId, exhibit)
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }

          // this.$http({
          //   url: `/exhibits/${!this.dataForm.exhibitId ? 'save' : 'update'}`,
          //   method: 'post',
          //   data: {
          //     exhibitId: this.dataForm.exhibitId || undefined,
          //     exhibitNo: this.dataForm.exhibitNo,
          //     exhibitName: this.dataForm.exhibitName,
          //     photo: this.dataForm.photo,
          //     exhibitCategories: this.dataForm.exhibitCategories,
          //     year: this.dataForm.year,
          //     unearthedSite: this.dataForm.unearthedSite,
          //     exhibitInfo: this.dataForm.exhibitInfo,
          //     praises: this.dataForm.praises,
          //     hits: this.dataForm.hits,
          //     createTime: this.dataForm.createTime,
          //     updateTime: this.dataForm.updateTime
          //   }
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.$message({
          //       message: '操作成功',
          //       type: 'success',
          //       duration: 1500,
          //       onClose: () => {
          //         this.visible = false
          //         this.$emit('refreshDataList')
          //       }
          //     })
          //   } else {
          //     this.$message.error(data.msg)
          //   }
          // })
        }
      })
    }
  }
}
</script>
